﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace MainDisplay
{
    public partial class Mosaic : Form
    {
        DateTime Start = DateTime.Now;
        DateTime Time1;
        
        object blank = null;
        EventArgs blah = null;
        private static SQLiteConnection sql_con;
        Boolean load1;

        public static void SetConnection()
        {
            // open the database
            String connectionString = "data source=" + "C:\\Mosaic\\Coefficients" +
          ";Version=3;";
            sql_con = new SQLiteConnection
                (connectionString);
        }
        public static void openDatabase()
        {
            SetConnection();
            if (sql_con.State != ConnectionState.Open)
            {
                try
                {

                    sql_con.Open();

                }

                catch (Exception)
                {

                    Console.WriteLine("not open");
                }
            }

        }
        public Mosaic()
        {
            InitializeComponent();
            Time1 = Start;
            Time1 = Time1.AddDays(-1);
            Time1.ToString();
            load1 = false;
          
            Mosaic_Load(blank, blah);
            timer1.Enabled = true; 
                   
        }

        
       private void Mosaic_Load(object sender, EventArgs e)
        {
           
            if (load1 == true) {
                return;
            }
         load1    = true ;
         
           // Refresh();
          //  chart1.Visible = false;
            openDatabase();
           DataTable dt = new DataTable();
         //  SQLiteDataReader queryCommandReader;
         //  SQLiteCommand cmd = new SQLiteCommand("SELECT Time, Power FROM Power WHERE(Time > " + Time1 + ")", sql_con);
         //  SQLiteCommand cmd = new SQLiteCommand("SELECT Time, Power FROM Power", sql_con);
           //queryCommandReader = cmd.ExecuteReader();
          // dt.Load(queryCommandReader);
        //   int i = dt.Rows.Count;
            
         //  SQLiteDataAdapter adpt = new SQLiteDataAdapter("@SELECT Time, Power FROM Power WHERE  ( > '" +Time1+"')", sql_con);
         //  this.coefficientsDataSet.AcceptChanges();
       
           //"@SELECT Time, Power FROM Power WHERE (strftime(%s, Time) > strftime(%s, Time1)");



           if (rbWeeks.Checked == true)
           {

               this.two_weeksTableAdapter.Fill(this.coefficientsDataSet1.two_weeks);
               dt = coefficientsDataSet1.two_weeks;
               bindingSource3 = new BindingSource();
               bindingSource3.DataSource = this.coefficientsDataSet1.two_weeks;

               bindingSource3.DataSource = dt;
               chart1.DataSource = bindingSource3;
               chart1.Series["Series1"].XValueMember = Convert.ToString(dt.Columns[0]);
               chart1.Series["Series1"].YValueMembers = Convert.ToString(dt.Columns[1]);

               chart1.DataBind();
               chart1.Visible = true;
               chart1.Invalidate();

               load1 = false;
           }
           else if (rbMonth.Checked == true)
           {
               this.monthTableAdapter.Fill(this.coefficientsDataSet1.month);
               dt = coefficientsDataSet1.month;
               bindingSource1 = new BindingSource();
               bindingSource1.DataSource = this.coefficientsDataSet1.month;
   
               bindingSource1.DataSource = dt;
               chart1.DataSource = bindingSource1;
               chart1.Series["Series1"].XValueMember = Convert.ToString(dt.Columns[0]);
               chart1.Series["Series1"].YValueMembers = Convert.ToString(dt.Columns[1]);

               chart1.DataBind();
               chart1.Visible = true;
               chart1.Invalidate();

               load1 = false;

           }
           else
           {
               this.powerTableAdapter.Fill(this.coefficientsDataSet1.Power);
               dt = coefficientsDataSet1.Power;
               bindingSource2 = new BindingSource();
               bindingSource2.DataSource = this.coefficientsDataSet1.Power;
               //bindingSource1.ResetBindings(false);
               bindingSource1.DataSource = dt;
               chart1.DataSource = bindingSource2;
               chart1.Series["Series1"].XValueMember = Convert.ToString(dt.Columns[0]);
               chart1.Series["Series1"].YValueMembers = Convert.ToString(dt.Columns[1]);

               chart1.DataBind();
               chart1.Visible = true;
               chart1.Invalidate();

               load1 = false;
           }
           
           return ;
        }


        private void rbWeeks_CheckedChanged(object sender, EventArgs e)
        {
            if (rbWeeks.Checked == true)
            {
                Time1 = Start;
                Time1 = Time1.AddDays(-14);
               // this.powerTableAdapter.Fill(this.coefficientsDataSet.two_weeks);
                Mosaic_Load(blank, blah);
                Console.WriteLine("Weeks = " +Time1);
            }
        }

        private void rbMonth_CheckedChanged(object sender, EventArgs e)
        {
           
        }

        private void rbNow_CheckedChanged_1(object sender, EventArgs e)
        {
            
        }

        private void chart1_Click(object sender, EventArgs e)
        {


        }

      

        private void timer1_Tick(object sender, EventArgs e)
        {
            Time1 = Time1.AddDays(-1);
            Mosaic_Load(sender , e);
          
        }

        private void Mosaic_Load_1(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'coefficientsDataSet1.two_weeks' table. You can move, or remove it, as needed.
            this.two_weeksTableAdapter.Fill(this.coefficientsDataSet1.two_weeks);
            // TODO: This line of code loads data into the 'coefficientsDataSet1.Power' table. You can move, or remove it, as needed.
            this.powerTableAdapter.Fill(this.coefficientsDataSet1.Power);
            // TODO: This line of code loads data into the 'coefficientsDataSet1.month' table. You can move, or remove it, as needed.
            this.monthTableAdapter.Fill(this.coefficientsDataSet1.month);

        }
      
    }
}
